package GUI;

import dbutils.DBHelper;
import service.CustomerService;
import service.MerchantService;
import service.ProducerService;
import utils.ValidationUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class registerFrame  extends JFrame{
    JFrame registerFrame = new JFrame("注册");
    JLabel nameLabel = new JLabel("用       户      名：");
    JLabel phoneLabel = new JLabel("手       机      号：");
    JLabel passwordLabel = new JLabel("密                 码：");
    JLabel apasswordLabel = new JLabel("再次输入密码：");
    JTextField nameField = new JTextField(20);
    JTextField phoneField = new JTextField(20);
    JTextField passwordField = new JTextField(20);
    JTextField apasswordField = new JTextField(20);
    JButton registerButtonC = new JButton("顾客注册");
    JButton registerButtonM = new JButton("商家注册");
    JButton registerButtonP = new JButton("生产商注册");
    JButton returnButton = new JButton("返回");
    JPanel namePanel = new JPanel();
    JPanel phonePanel = new JPanel();
    JPanel passwordPanel = new JPanel();
    JPanel apasswordPanel = new JPanel();
    JPanel buttonPanel = new JPanel();

    public void registerFrame() {
        namePanel.add(nameLabel);
        namePanel.add(nameField);
        phonePanel.add(phoneLabel);
        phonePanel.add(phoneField);
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);
        apasswordPanel.add(apasswordLabel);
        apasswordPanel.add(apasswordField);
        buttonPanel.add(registerButtonC);
        buttonPanel.add(registerButtonM);
        buttonPanel.add(registerButtonP);
        buttonPanel.add(returnButton);

        registerFrame.setLayout(new GridLayout(5, 1));
        registerFrame.add(namePanel);
        registerFrame.add(phonePanel);
        registerFrame.add(passwordPanel);
        registerFrame.add(apasswordPanel);
        registerFrame.add(buttonPanel);
        registerFrame.setSize(500, 300);
        registerFrame.setLocationRelativeTo(null);
        registerFrame.setVisible(true);
        registerFrame.setDefaultCloseOperation(2);



        //顾客注册
        registerButtonC.addActionListener(e -> {
            String name = nameField.getText();
            String phone = phoneField.getText();
            String password = passwordField.getText();
            String apassword = apasswordField.getText();

            // 检查手机号格式
            if (!phone.matches("\\d{11}")) {
                // 手机号格式不正确，请重新输入
                JOptionPane.showMessageDialog(null, "手机号格式不正确，请重新输入");
                return;
            }

            // 验证两次密码是否一致
            String pwd = ValidationUtil.getPassword(password, apassword);
            if (pwd == null) {
                // 如果密码不一致则结束方法
                return;
            }

            // 检查用户名是否存在
            if (CustomerService.isUsernameExists(name)) {
                // 用户名已存在，请重新输入
                JOptionPane.showMessageDialog(null, "用户名已存在，请重新输入");
                return;
            }

            // 检查手机号是否存在
            if (CustomerService.isPhoneExists(phone)) {
                // 手机号已存在，请重新输入
                JOptionPane.showMessageDialog(null, "手机号已存在，请重新输入");
                return;
            }

            // 注册用户
            CustomerService customerService = new CustomerService();
            customerService.signIn(name, phone, password, apassword);

            registerFrame.dispose();
            new Menu().Menu();
        });

        //商家注册
        registerButtonM.addActionListener(e -> {
            String name = nameField.getText();
            String phone = phoneField.getText();
            String password = passwordField.getText();
            String apassword = apasswordField.getText();

            // 检查手机号格式
            if (!phone.matches("\\d{11}")) {
                // 手机号格式不正确，请重新输入
                JOptionPane.showMessageDialog(null, "手机号格式不正确，请重新输入");
                return;
            }

            // 验证两次密码是否一致
            String pwd = ValidationUtil.getPassword(password, apassword);
            if (pwd == null) {
                // 如果密码不一致则结束方法
                return;
            }

            // 检查用户名是否存在
            if (MerchantService.isUsernameExists(name)) {
                // 用户名已存在，请重新输入
                JOptionPane.showMessageDialog(null, "用户名已存在，请重新输入");
                return;
            }

            // 检查手机号是否存在
            if (MerchantService.isPhoneExists(phone)) {
                // 手机号已存在，请重新输入
                JOptionPane.showMessageDialog(null, "手机号已存在，请重新输入");
                return;
            }

            // 注册用户
            MerchantService merchantService = new MerchantService();
            merchantService.signIn(name, phone, password, apassword);

            registerFrame.dispose();
            new Menu().Menu();
        });

        //生产商注册
        registerButtonP.addActionListener(e -> {
            String name = nameField.getText();
            String phone = phoneField.getText();
            String password = passwordField.getText();
            String apassword = apasswordField.getText();

            // 检查手机号格式
            if (!phone.matches("\\d{11}")) {
                // 手机号格式不正确，请重新输入
                JOptionPane.showMessageDialog(null, "手机号格式不正确，请重新输入");
                return;
            }

            // 验证两次密码是否一致
            String pwd = ValidationUtil.getPassword(password, apassword);
            if (pwd == null) {
                // 如果密码不一致则结束方法
                return;
            }

            // 检查用户名是否存在
            if (ProducerService.isUsernameExists(name)) {
                // 用户名已存在，请重新输入
                JOptionPane.showMessageDialog(null, "用户名已存在，请重新输入");
                return;
            }

            // 检查手机号是否存在
            if (ProducerService.isPhoneExists(phone)) {
                // 手机号已存在，请重新输入
                JOptionPane.showMessageDialog(null, "手机号已存在，请重新输入");
                return;
            }

            // 注册用户
            ProducerService producerService = new ProducerService();
            producerService.signIn(name, phone, password, apassword);

            registerFrame.dispose();
            new Menu().Menu();
        });

        returnButton.addActionListener(e -> {
                    registerFrame.dispose();
                    new Menu().Menu();
                }
        );






    }
}




